package com.next

import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*
import grails.gorm.transactions.Transactional

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CUSTOMER_SERVICE_REPRESENTATIVE', 'ROLE_ACCOUNT_MANAGER', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_GENERAL_RISK_MANAGER', 'ROLE_BRANCH_GENERAL_MANAGER', 'ROLE_BRANCH_OFFICE_RISK_MANAGER', 'ROLE_HEAD_OFFICE_RISK_MANAGER', 'ROLE_GENERAL_MANAGER', 'ROLE_CRO', 'ROLE_CEO', 'ROLE_POST_LOAN_MANAGER', 'ROLE_BRANCH_OFFICE_POST_LOAN_MANAGER'])
@Transactional(readOnly = true)
class CertificateController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index()
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        def max = params.max
        def offset = params.offset
        def sort = params["sort"]
        def order = params["order"]
        def account = params["account"]
        def searchSql = " from Certificate c where 1=1 "
        def sql = " "
        if (account)
        {
            sql += " and c.account.name like '%${account}%'"
        }
        def countSql = " select count(c.id) from Certificate c where 1=1 " + sql
        if (order && sort)
        {
            sql += " order by c.${sort} ${order}"
        }
        else
        {
            sql += "order by c.modifiedDate desc "
        }
        searchSql += sql
        def certificateList = Certificate.executeQuery(searchSql, [max: max, offset: offset])
        def count = Certificate.executeQuery(countSql)

        respond certificateList, model: [certificateCount: count[0], params: params]
    }

    def show(Certificate certificate)
    {
        respond certificate
    }

    def create()
    {
        def targetUri = request.getHeader("referer")
        def accountId = params.account
        respond new Certificate(params), model: [accountId: accountId, targetUri: targetUri]
    }

    @Transactional
    def save(Certificate certificate)
    {
        if (certificate == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (certificate.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond certificate.errors, model: [targetUri: params['targetUri'], accountId: certificate?.account?.id], view: 'create'
            return
        }

        certificate.save flush: true

        redirect controller: "certificate", action: "index"
    }

    def edit(Certificate certificate)
    {
        def targetUri = request.getHeader("referer")
        respond certificate, model: [targetUri: targetUri]
    }

    @Transactional
    def update(Certificate certificate)
    {
        if (certificate == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (certificate.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond certificate.errors, model: [targetUri: params['targetUri']], view: 'edit'
            return
        }

        certificate.save flush: true
        redirect controller: "certificate", action: "show", method: "GET", id: certificate?.id

    }

    @Transactional
    def delete(Certificate certificate)
    {

        if (certificate == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        certificate.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'certificate.label', default: 'Certificate'), certificate.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'certificate.label', default: 'Certificate'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
